package org.videopractice.media;

import org.videopractice.common.FileUtils;
import org.videopractice.common.Logger;
import org.videopractice.media.MediaEncoder.MediaDataHandler;
import org.videopractice.network.ServerManager;
import org.videopractice.network.SupplierServer;
import org.videopractice.network.message.Message;
import org.videopractice.network.message.MessageCode;
import org.videopractice.network.message.request.LiveDataRequest;

import java.util.Arrays;
import java.util.concurrent.LinkedBlockingQueue;

public class ScreenLive extends Thread implements MediaDataHandler {

    private final LinkedBlockingQueue<MediaDataPackage> dataPackageQueue = new LinkedBlockingQueue<>();
    private final SupplierServer supplierServer = new SupplierServer();
    private final ServerManager serverManager = new ServerManager();

    private boolean isLiving = true;

    private long startTime;

    private byte[] sps, pps;

    public ScreenLive() { }

    public ScreenLive startLiving() {
        supplierServer.connect();
        start();
        return this;
    }

    @Override
    public void doHandler(MediaDataPackage dataPackage) {
        if(!isLiving) {
            return;
        }

        if(startTime == 0) {
            startTime = dataPackage.timestamp;
        }

        FileUtils.writeFileToByte(dataPackage.mediaData);
        FileUtils.writeFileToString(dataPackage.mediaData);

        dataPackageQueue.add(dataPackage);
    }

    private void postData(byte[] data) {
        Message message = new Message(MessageCode.LiveDataRequest, new LiveDataRequest(data).toJson());
        serverManager.sendMessageToAll(message);
    }

    private void sendScreenData(byte[] mediaData, long timestamp) {
        // mediaData 一个包就是一个分隔符间的内容，视为一帧内容，所以只需要判断分隔符的类型为[4]
        if(mediaData[4] == 0x67) {
            Logger.d("---------------------------找到sps--------------------------------");
            if(sps == null || pps == null) {
                Logger.d("---------------------------缓存sps--------------------------------");
                for(int index = 0; index < mediaData.length; index++) {
                    if(mediaData[index] == 0x00 && mediaData[index + 1] == 0x00
                            && mediaData[index + 2] == 0x00 && mediaData[index + 3] == 0x01
                            && mediaData[index + 4] == 0x68) {
                        sps = Arrays.copyOfRange(mediaData, 0, index); // 直播推流不需要分隔符，所以要起始 +4
                        pps = Arrays.copyOfRange(mediaData, index, mediaData.length); // 直播推流不需要分隔符，所以要起始 +4
                    }
                }
            }
        }

        // 关键帧需要发送相关的 sps 和 pps数据
        if(mediaData[4] == 0x65) {
            postData(sps);
            postData(pps);
        }

        // 发送关键帧数据
        postData(mediaData);
    }

    @Override
    public void run() {
        while (isLiving) {
            try {
                MediaDataPackage mediaDataPackage = dataPackageQueue.take();
                if(mediaDataPackage != null && !mediaDataPackage.isEmpty()) {
                    sendScreenData(mediaDataPackage.mediaData, mediaDataPackage.timestamp - startTime);
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

}
